/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.command;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.commons.view.Interface;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;

/**
 * This class ia an implementation of Command interface.
 * Its "execute" method gets the current point of DrawingArea
 * and sets its Shape as a line of a given initial point to
 * this new one.
 *
 * The initial point must be set wen this class is instantiated,
 * otherwise an NullPointerException will occur.
 *
 * It does exists to be set as a command of the DrawingArea to do
 * set the Shape at run time in order to create a preview when adding
 * a line to the model.
 *
 * <br>Example:<br>
 * <pre>
 * PreviewLineCommand pl = new PreviewLineCommand(parent);
 * pl.setInitialPoint(pt0);
 * drawingArea.setMovedCommand(pl);
 *</pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see    DrawingArea#getViewPoint
 * @see    DrawingArea#setShape
 * @see    DrawingArea#mouseMoved
 */
public class PreviewZoomCommand implements Command {
    
    private InterfacePostp parent;
    private Point init;
    
//*****************************************************************************
	
    /**
     * Creates new PreviewZoomCommand and gets the
     * DrawingArea from parent.
     *
     * @param   parent The parent JFrame
     * @see   Interface#getDrawing
     */
    public PreviewZoomCommand(JInternalFrame parent) {
        this.parent = (InterfacePostp)parent;
    }
    
//*****************************************************************************
	
    /**
     * Gets the DrawingArea current view point and sets
     * its Shape as a line from the initial set point to this one.
     */
    public void execute() {
        try{
     
            Point fin = parent.getView().getDrawingArea().getViewPoint();
            parent.getView().getDrawingArea().setShape(new Rectangle(Math.min(init.x, fin.x), Math.min(init.y, fin.y), Math.abs(fin.x - init.x), Math.abs(fin.y - init.y)));
            
        }catch (NullPointerException e) {
            Toolkit.getDefaultToolkit().beep();
            System.out.print(e.toString());
            JOptionPane.showMessageDialog(parent, e.toString(), "ERROR Message", JOptionPane.ERROR_MESSAGE);
        }
    }
    
//*****************************************************************************
	
    /**
     * Sets the initial point to the preview line.
     *
     * @param    po    The desired point
     * @see    java.awt.Point
     */
    public void setInitialPoint(Point po) {
        init = po;
    }
    
//*****************************************************************************
	
    /**
     * Impossible to undo - not implemented.
     */
    public void undo() {}
    
//*****************************************************************************
	
    /**
     * Impossible to redo - not implemented.
     */
    public void redo() {}
    
//*****************************************************************************
}
